package top.zhdev.community.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.zhdev.community.entity.User;
import top.zhdev.community.service.FollowService;
import top.zhdev.community.service.UserService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.RedisKeyUtil;

import javax.annotation.Resource;
import java.util.*;

/**
 * 关注业务处理
 */
@Service
public class FollowServiceImpl implements FollowService, CommunityConstant {
    @Resource
    private RedisTemplate redisTemplate;

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    /**
     * 查询用户关注了多少实体
     * @param userId
     * @param entityType
     * @return
     */
    @Override
    public long getFolloweeCount(int userId, int entityType) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
        return redisTemplate.opsForZSet().zCard(followeeKey);
    }

    /**
     * 查询用户的粉丝数量
     * @param userId
     * @param entityType
     * @return
     */
    @Override
    public long getFollowerCount(int userId, int entityType) {
        String followerKey = RedisKeyUtil.getFollowerKey(entityType, userId);
        return redisTemplate.opsForZSet().zCard(followerKey);
    }

    /**
     * 查询该用户是否已经关注该实体
     * @param userId
     * @param entityType
     * @param entityId
     * @return
     */
    @Override
    public boolean hasFollowed(Integer userId, int entityType, int entityId) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
        // 通过查询该实体的分数，判断是否存在
        return  redisTemplate.opsForZSet().score(followeeKey,entityId) != null;
    }

    /**
     * 查询该用户关注的实体信息
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Map<String, Object>> getFollowees(int userId, int offset, int limit) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(userId,ENTITY_TYPE_USER);
        Set<Integer> targetIds = redisTemplate.opsForZSet().range(followeeKey,offset,offset + limit - 1);
        if(targetIds == null){
            return null;
        }

        List<Map<String,Object>> list = new ArrayList<>();
        for(Integer targetId : targetIds){
            Map<String,Object> map = new HashMap<>();
            User user = userService.getUserById(targetId);
            map.put("user",user);
            Double score = redisTemplate.opsForZSet().score(followeeKey,targetId);
            map.put("followTime",new Date(score.longValue()));
            list.add(map);
        }
        return list;
    }

    /**
     * 查询该用户的粉丝信息
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Map<String, Object>> getFollowers(int userId, int offset, int limit) {
        String followerKey = RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER,userId);
        Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey,offset,offset + limit - 1);
        if(targetIds == null){
            return null;
        }

        List<Map<String,Object>> list = new ArrayList<>();
        for(Integer targetId : targetIds){
            Map<String,Object> map = new HashMap<>();
            User user = userService.getUserById(targetId);
            map.put("user",user);
            Double score = redisTemplate.opsForZSet().score(followerKey,targetId);
            map.put("followTime",new Date(score.longValue()));
            list.add(map);
        }

        return list;
    }
}
